import com.adobe.cairngorm.control.*;
import com.wcarmon.life.model.LifeModelLocator;
import com.wcarmon.life.view.*;
import com.wcarmon.life.event.*;
import com.wcarmon.life.vo.*;
import com.wcarmon.utils.*;
import mx.controls.Alert;
import mx.events.*;
import mx.managers.*;
import mx.validators.*;
import mx.utils.*;
import mx.collections.*;
import mx.binding.utils.ChangeWatcher;

include "../../../includes/loggerSnippet.as";


[Bindable]
private var model : LifeModelLocator = LifeModelLocator.getInstance();
 
public var currentFieldGroup : Object;
public var currentBlockState : String = "";
   
   
   
   
////////////////////////////////////////////////   
private function doInit() : void{
	ChangeWatcher.watch( model, "blockToProcessHasLoaded", handleBlockLoading );
	ChangeWatcher.watch( this, "currentState", handleStateChange );
	BlockContent.setFocus();
}
   
public function handleBlockLoading( event:Event ) : void {
	if( model.blockToProcessHasLoaded ){
		log.debug('block for processing has loaded');
		initAllFieldValues( model.currentProcessBlock );
	}else{
		log.debug('still waiting for block to load for processing');
	}
}


public function handleStateChange( event : Event ): void{
	log.fatal('Givon: test this..state changed!');
	BlockContent.setFocus();
	currentFieldGroup.setValues( model.currentProcessBlock );
}


private function initAllFieldValues( b : BlockVO ) : void {
	BlockContent.setValue( b.content );
	BlockProjectIds.setValue( b.projectIds );
	BlockRefMeta.setValue( b.ref_meta );
	BlockContent.setFocus();
	currentFieldGroup.setValues( b );
}


public function handleSaveClick( e:Event ) : void {
	//Validate common fields
	if( !BlockContent.isValid() ){ return; }
	if( !BlockProjectIds.isValid() ){ return; }
	if( !BlockRefMeta.isValid() ){ return; }
	log.debug('Common fields are valid');
	
	//Validate state-specific fields
	if( !currentFieldGroup.formIsValid() ){
		log.debug('form is invalid somewhere in there...');
		return;
	}
	
	log.info('About to save (Process)');
	var b : BlockVO = currentFieldGroup.getValues();
	log.debug('Got this from state-specific fields: ' + ObjectUtil.toString(b) );
	
	//Set common field values
	b.content = BlockContent.getValue();
	b.projectIds = BlockProjectIds.getValue();
	b.ref_meta = BlockRefMeta.getValue();
	b.id = model.currentProcessBlock.id;
	
	log.debug("About to save this: " + ObjectUtil.toString(b) );
	
	( new SaveBlockEvent(b, this) ).dispatch();
}


////////////////////////////////////////////////
private function handleSaveCallback() : void{
	log.debug('Save callback was successful!');
	//clearValues();
	//Givon: update stats??
	( new ShowProcessEvent() ).dispatch();	//process 1st item in list
}

/*
private function clearValues() : void{
	this.currentState="NextActionMode";
	BlockContent.clearValue();
	BlockProjectIds.clearValue();
	BlockRefMeta.clearValue();
	currentFieldGroup.clearValues();
}
*/